<?php


/** ensure this file is being included by a parent file */
defined( '_JEXEC' ) or die( 'Direct Access to this location is not allowed.' );

/**
 * @package	
 */
class HelpDeskText {
	
	/**
	 * Wrapper that translates a string into the current language
	 *
	 * @param	string $string The string to translate
	 * @param	boolean	$jsSafe		Make the result javascript safe
	 *
	 */
	function &_($string, $jsSafe = false) {
		$return = "";
		
		if (HelpDeskHelperUtility::class_exists( 'JText' )) {
			$return = JText::_( $string );				
		} else {
			$prefix = strtoupper( "_HelpDesk_" );
			$key = strtoupper($string);

			// trim whitespace
			$trim_key = trim( str_replace( " ", "", $key ) );
			// strip all html tags
			$wc = strip_tags($trim_key);
			// remove 'words' that don't consist of alphanumerical characters or punctuation
			$pattern = "#[^(\w|\d|\'|\"|\.|\!|\?|;|,|\\|\/|\-|:|\&|@)]+#";
			$wc = trim(preg_replace($pattern, "", $wc));
			// remove one-letter 'words' that consist only of punctuation
			$wc = trim(preg_replace("#\s*[(\'|\"|\.|\!|\?|;|,|\\|\/|\-|:|\&|@)]\s*#", "", $wc));

			$constant = $prefix.$wc;					
			if ($prefix != $key && strval($key) != "") {
				$return = defined( $constant ) ? constant( $constant ) : strval( $string );
			} elseif ($prefix == $key) {
				$return = defined( $prefix ) ? constant( $prefix ) : strval( $string );
			}

		}
		
		return $return;
	}
	
	/**
	 * 
	 * @param $string
	 * @return unknown_type
	 */
	function sprintf($string)
	{
		// $lang =& JFactory::getLanguage();
		$args = func_get_args();
		if (count($args) > 0) {
			$args[0] = HelpDeskText::_($args[0]);
			return call_user_func_array('sprintf', $args);
		}
		return '';
	}

	/**
	 * Passes a string thru an printf
	 *
	 * @access	public
	 * @param	format The format string
	 * @param	mixed Mixed number of arguments for the sprintf function
	 * @since	1.5
	 */
	function printf($string)
	{
		// $lang =& JFactory::getLanguage();
		$args = func_get_args();
		if (count($args) > 0) {
			$args[0] = HelpDeskText::_($args[0]);
			return call_user_func_array('printf', $args);
		}
		return '';
	}

}

?>